package com.cn.medicalcare.service.serviceimpl;

import com.cn.medicalcare.customtool.JedisUtil;
import com.cn.medicalcare.customtool.JsonTool;
import com.cn.medicalcare.customtool.SqlSessionFactoryTool;
import com.cn.medicalcare.mapper.AdvertisementMapper;
import com.cn.medicalcare.mapper.NavigationMapper;
import com.cn.medicalcare.pojo.Advertisement;
import com.cn.medicalcare.pojo.AllPage;
import com.cn.medicalcare.pojo.Navigation;
import com.cn.medicalcare.service.AdvertisementService;
import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

public class AdvertisementServiceImpl implements AdvertisementService {

    private static final String AD_CACHE_KEY = "survival_advertisement";

    //静态内部类单例模式
    private static class SingletonClassInstance {
        private static final AdvertisementServiceImpl instance = new AdvertisementServiceImpl();
    }

    private AdvertisementServiceImpl() {
    }

    public static AdvertisementServiceImpl getInstance() {
        return AdvertisementServiceImpl.SingletonClassInstance.instance;
    }


    @Override
    public List<Advertisement> selectSurvivalAdvertisement() {
        List<Advertisement> list = null;
        try {
            String adjson = JedisUtil.jedisDo(j -> j.get(AD_CACHE_KEY));
            if (StringUtils.isNotEmpty(adjson)) {
                list = JsonTool.toCollection(adjson, new TypeReference<List<Advertisement>>() {
                });
                JedisUtil.jedisDo(j -> j.expire(AD_CACHE_KEY, JedisUtil.CACHE_TIME));
            }
        } catch (Exception e) {
        }
        if (list == null) {
            list = SqlSessionFactoryTool.DoMapper(AdvertisementMapper.class, m -> m.selectSurvivalAdvertisement());
            try {
                String adjson = JsonTool.toJson(list);
                JedisUtil.jedisDo(j -> j.setex(AD_CACHE_KEY,JedisUtil.CACHE_TIME, adjson));
            } catch (Exception e) {
            }
        }
        return list;
    }

    @Override
    public int selectAdvertisementCount(Advertisement advertisement) {
        return SqlSessionFactoryTool.DoMapper(AdvertisementMapper.class, m -> m.selectAdvertisementCount(advertisement));
    }

    @Override
    public List<Advertisement> selectAdvertisementInPage(AllPage<Advertisement> allPage) {
        List<Advertisement> list = SqlSessionFactoryTool.DoMapper(AdvertisementMapper.class, m -> m.selectAdvertisementInPage(allPage));
        return list;
    }

    @Override
    public boolean insertAdvertisement(Advertisement advertisement) {
        advertisement.setSurvivalupdatetime(new Date());
        boolean insertFlag = SqlSessionFactoryTool.DoMapperAndCommit(AdvertisementMapper.class, m -> m.insertAdvertisement(advertisement));
        if (insertFlag) {
            JedisUtil.jedisDo(j -> j.del(AD_CACHE_KEY));
        }
        return insertFlag;
    }

    @Override
    public boolean updateAdvertisement(Advertisement advertisement) {
        if (advertisement.getSurvival() == 1) {
            advertisement.setSurvivalupdatetime(new Date());
        }
        boolean updateFlag = SqlSessionFactoryTool.DoMapperAndCommit(AdvertisementMapper.class, m -> m.updateAdvertisement(advertisement));
        if (updateFlag) {
            JedisUtil.jedisDo(j -> j.del(AD_CACHE_KEY));
        }
        return updateFlag;
    }

}
